/*******************************************************************************
 * Copyright 2017 Bstek
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package com.bstek.ureport.parser.impl.value;

import org.dom4j.Element;

import com.bstek.ureport.definition.value.Source;
import com.bstek.ureport.definition.value.ZxingCategory;
import com.bstek.ureport.definition.value.ZxingValue;
import com.bstek.ureport.expression.ExpressionUtils;
import com.bstek.ureport.expression.model.Expression;
import com.bstek.ureport.parser.Parser;

/**
 * @author Jacky.gao
 * @since 2017年3月6日
 */
public class ZxingValueParser implements Parser<ZxingValue> {
	
	public final static Parser<ZxingValue> instance = new ZxingValueParser();

	@Override
	public ZxingValue parse(Element element) {
		ZxingValue value = new ZxingValue();
		Source source = Source.valueOf(element.attributeValue("source"));
		value.setSource(source);
		value.setWidth(Integer.valueOf(element.attributeValue("width")));
		value.setHeight(Integer.valueOf(element.attributeValue("height")));
		value.setFormat(element.attributeValue("format"));
		value.setCategory(ZxingCategory.valueOf(element.attributeValue("category")));
		value.setCodeDisplay(Boolean.valueOf(element.attributeValue("code-display")));
		for (Object obj : element.elements()) {
			if (obj == null || !(obj instanceof Element)) {
				continue;
			}
			Element ele = (Element) obj;
			if (ele.getName().equals("text")) {
				if (source.equals(Source.text)) {
					value.setText(ele.getText());
				} else {
					value.setExpr(ele.getText());
				}
				break;
			}
		}
		if (source.equals(Source.expression)) {
			String expr = value.getExpr();
			Expression expression = ExpressionUtils.parseExpression(expr);
			value.setExpression(expression);
		}
		return value;
	}

	@Override
	public boolean support(String name) {
		return name.equals("zxing-value");
	}
}
